package com.mrjoew.weekend.web;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.mrjoew.weekend.configurations.ScaffoldProperties;
import com.mrjoew.weekend.entities.Response;
import com.mrjoew.weekend.services.sys.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author Christopher Jones
 * @explain
 * @date 2020/11/6 0006 23:43
 */
@Api(tags = "菜单")
@RequestMapping(value = "/sysapi/menu")
@RestController
public class MenuApiController {

    private final ScaffoldProperties scaffoldProperties;

    private final SysMenuService sysMenuService;

    public MenuApiController(SysMenuService sysMenuService, ScaffoldProperties scaffoldProperties) {
        this.sysMenuService = sysMenuService;
        this.scaffoldProperties = scaffoldProperties;
    }

    @ApiOperation("菜单列表")
    @GetMapping(value = {"", "/list"})
    public String list() {

        return "";
    }

    @ApiOperation("上传菜单Excel")
    @PostMapping(value = "/upload")
    public Response<Object> upload(@RequestParam("file") MultipartFile file) {

        return Response.ok();
    }

    @ApiOperation("下载菜单上传模板")
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response) throws IOException {
        String xlsxSavePath = String.format("%s/菜单上传模板.xlsx", scaffoldProperties.getBaseFilePath());

        // 模板文件
        File xlsxTemplate = new File(xlsxSavePath);
        // 如果模板文件不存在
        if (!xlsxTemplate.exists()) {
            List<String> row1 = CollUtil.newArrayList("菜单名称", "上级菜单名称(如是顶级菜单不用填写)", "URL", "ICON地址", "排序");
            List<String> row2 = CollUtil.newArrayList("", "", "", "", "");

            List<List<String>> rows = CollUtil.newArrayList(row1, row2);
            for (int i = 0; i < 100; i++) {
                rows.add(row2);
            }

            File file = new File(scaffoldProperties.getBaseFilePath());
            if (!file.exists()) {
                boolean mkdir = file.mkdir();
                if (!mkdir) {
                    response.getWriter().write("<script>alert('模板下载失败, 请联系管理员')</script>");
                    return;
                }
            }

            // 通过工具类创建writer
            ExcelWriter writer = ExcelUtil.getWriter(xlsxSavePath);
            // 通过构造方法创建writer
            // ExcelWriter writer = new ExcelWriter("d:/writeTest.xls");

            // 跳过当前行，既第一行，非必须，在此演示用
            writer.passCurrentRow();

            // 合并单元格后的标题行，使用默认标题样式
            writer.merge(row1.size() - 1, "菜单上传模板");
            // 一次性写出内容，强制输出标题
            writer.write(rows, true);
            // 关闭writer，释放内存
            writer.close();
        }
        /*else {
            OutputStream os = response.getOutputStream();
            writer.flush(os, true);
            writer.close();
            IoUtil.close(os);
        }*/

        try (InputStream is = new FileInputStream(xlsxSavePath); OutputStream os = response.getOutputStream()) {
            response.setHeader("Content-Disposition",
                    "attachment; filename=" + URLEncoder.encode("商品上传模板.xlsx", "UTF-8"));

            int len = 0;
            byte[] buffer = new byte[1024];

            while ((len = is.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }
        }
    }
}
